package com.mspbots.api.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.mspbots.api.service.TenantService;
import com.mspbots.cw.request.TenantDTO;
import com.mspbots.dto.page.IPage;
import com.mspbots.dto.page.Page;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * com.mspbots.api.service.impl.TenantServiceImpl
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/6/28
 */
@Deprecated
@Service
public class TenantServiceImpl extends BaseGenericService implements TenantService {

    @Override
    public List<TenantDTO> addBatch(List<TenantDTO> dtos) {
        Mono<JsonNode> resultMono = this.apiClient.post("/tenant/add-batch", dtos);
        JsonNode result = resultMono.block();
        dtos = this.objectMapper.convertValue(result, new TypeReference<>() {
        });
        return dtos;
    }


    @Override
    public List<TenantDTO> list(Map<String, Object> params) {
        try {
            Mono<JsonNode> resultMono = this.apiClient.get("/tenant/list", params);
            JsonNode result = resultMono.block();
            return this.objectMapper.convertValue(result, new TypeReference<>() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IPage<TenantDTO> selectPage(Map<String, Object> params) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/page", params);
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, new TypeReference<Page<TenantDTO>>() {
        });
    }

    @Override
    public TenantDTO findById(long id) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/" + id + "/info");
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, TenantDTO.class);
    }

    @Override
    public TenantDTO save(TenantDTO entity) {
        Mono<JsonNode> resultMono = this.apiClient.post("/tenant", entity);
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, TenantDTO.class);
    }

    @Override
    public TenantDTO update(TenantDTO entity) {
        Mono<JsonNode> resultMono = this.apiClient.put("/tenant/" + entity.getId(), entity);
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, TenantDTO.class);
    }

    @Override
    public Boolean delete(Long id) {
        Mono<JsonNode> resultMono = this.apiClient.delete("/tenant/" + id, null);
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, Boolean.class);
    }

    @Override
    public TenantDTO findByName(String name) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/name/" + name);
        JsonNode result = resultMono.block();
        if ((result != null ? result.size() : 0) > 0) {
            return this.objectMapper.convertValue(result, TenantDTO.class);
        } else {
            return null;
        }
    }

}
